using UnityEngine;
using System.Collections;
using System.Collections.Generic;

namespace Model
{
    public class BallSimulation
    {
        private float levelWidth = 1.0f;
        private float levelHeight = 1.0f;
        private float borderSize = 0.03f;

        public List<Ball> GetBallList { get { return ballList; } }
        public float GetBorderSize { get { return borderSize; } }
        public float GetLevelWidth { get { return levelWidth; } }
        public float GetLevelHeight { get { return levelHeight; } }

        private List<Ball> ballList = new List<Ball>();

        public BallSimulation(int numberOfBalls)
        {
            for (int i = 0; i < numberOfBalls; i++)
            {
                ballList.Add(new Ball(i));
            }

            foreach (var ball in ballList)
            {
                // Where all the walls is in logical position.
                ball.wallPositionRight = levelWidth - borderSize - ball.Radius;
                ball.wallPositionBottom = levelHeight - borderSize - ball.Radius;
                ball.wallPositionLeft = 0 + borderSize + ball.Radius;
                ball.wallPositionTop = 0 + borderSize + ball.Radius;
            }
        }

        /// <summary>
        /// Call on this in MasterController.Update() to run physics logic for ball.
        /// </summary>
        public void Update()
        {
            foreach (var ball in ballList)
            {
                reflectDirectionOnCollision(ball);
                updatePosition(ball);   
            }
        }

        /// <summary>
        /// Update position.
        /// </summary>
        /// <param name="ball"></param>
        private void updatePosition(Ball ball)
        {
            ball.position.x += Time.deltaTime * ball.velocity.x;
            ball.position.y += Time.deltaTime * ball.velocity.y;
        }

        /// <summary>
        /// Change velocity direction on collision.
        /// </summary>
        /// <param name="ball"></param>
        private void reflectDirectionOnCollision(Ball ball)
        {
            if (ball.Position.x > ball.wallPositionRight && ball.savedCollision != Wall.right)
            {
                ball.velocity.x *= -1.0f;
                ball.savedCollision = Wall.right;
            }

            if (ball.Position.y > ball.wallPositionBottom && ball.savedCollision != Wall.bottom)
            {
                ball.velocity.y *= -1.0f;
                ball.savedCollision = Wall.bottom;
            }

            if (ball.Position.x < ball.wallPositionLeft && ball.savedCollision != Wall.left)
            {
                ball.velocity.x *= -1.0f;
                ball.savedCollision = Wall.left;
            }

            if (ball.Position.y < ball.wallPositionTop && ball.savedCollision != Wall.top)
            {
                ball.velocity.y *= -1.0f;
                ball.savedCollision = Wall.top;
            }
        }
    }
}